//
//  ShouCang.m
//  SeeTheWorld
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ShouCang.h"
#import "ShouCangCell.h"
#import "DataBaseHandler.h"
#import "ModelOfClickZhuanTiCell.h"
#import "VCForClickZhuanTiCell.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


@interface ShouCang () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;

@end

@implementation ShouCang

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_array release];
    [_tableView release];
    [_buttonOfLeft release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"收藏";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil];
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    [self createTableView];
}


- (void)viewWillAppear:(BOOL)animated {
    
    self.array = [NSMutableArray array];
    
    self.array = [[DataBaseHandler shareDataBaseHandler] shouCang];
    
    if (self.array.count == 0) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"KongDangDang"]];
        imageView.frame = CGRectMake(WIDTH / 3, HEIGHT / 4, WIDTH / 3, WIDTH / 3);
        [self.view addSubview:imageView];
        [imageView release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT / 4 + WIDTH / 3, WIDTH / 3, 40)];
        [self.view addSubview:label];
        label.text = @"这里空荡荡";
        label.textAlignment = 1;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:20];
        label.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);

        
    }
    
    [self.tableView reloadData];
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[ShouCangCell class] forCellReuseIdentifier:@"pool"];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShouCangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    ModelOfClickZhuanTiCell *model = [self.array objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    return cell;
}

/** 使tableView进入编辑状态. */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:YES];
    
}

/** 删除cell */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ModelOfClickZhuanTiCell *model = [self.array objectAtIndex:indexPath.row];
        
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

        [[DataBaseHandler shareDataBaseHandler] deleteWithName:model.name];
        
    }
    
    if (self.array.count == 0) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"KongDangDang"]];
        imageView.frame = CGRectMake(WIDTH / 3, HEIGHT / 4, WIDTH / 3, WIDTH / 3);
        [self.view addSubview:imageView];
        [imageView release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT / 4 + WIDTH / 3, WIDTH / 3, 40)];
        [self.view addSubview:label];
        label.text = @"这里空荡荡";
        label.textAlignment = 1;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:20];
        label.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);
        
    }
}

/** 点击cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelOfClickZhuanTiCell *model = [self.array objectAtIndex:indexPath.row];
    
    VCForClickZhuanTiCell *vc = [[VCForClickZhuanTiCell alloc] init];
    vc.id = model.id;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}




- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
